package team.sun.sss.controller.extra;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.sun.sss.dao.extra.ChangeWorkDao;
import team.sun.sss.dao.extra.ExtraDao;
import team.sun.sss.dao.staff.StaffDao;
import team.sun.sss.pojo.changework.ChangeWork;
import team.sun.sss.pojo.extra.ExtraStaff;
import team.sun.sss.pojo.extra.ExtraWork;
import team.sun.sss.pojo.staff.Staff;
import team.sun.sss.pojo.user.User;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

@Controller
public class ExtraController {
    @Autowired
    ExtraDao extraDao;
    @Autowired
    StaffDao staffDao;
    @Autowired
    ChangeWorkDao changeWorkDao;

    @GetMapping("/extra_index2")
    @ResponseBody
    public List<ExtraWork> extraIndex2(){
        return extraDao.selectAll();
    }

    //值班管理
    @GetMapping("/extra_index")
    public String extraIndex1(){
        return "extra/extra_index";
    }

    //教师端值班表
    @GetMapping("/staff_extra")
    public String staffExtra(){
        return "extra/staff_extra";
    }

    //获取所有的值班人员
    @GetMapping("/get_all_staff")
    @ResponseBody
    public List<Staff> getAllStaff(){
        return staffDao.getStaffList();
    }

    //添加值班表
    @PostMapping("insert_extra_work")
    @ResponseBody
    public void insertExtraWork(@RequestParam Date date,@RequestParam String[] staffs,HttpSession session){
        for (String staff : staffs) {
            ExtraStaff extraStaff = new ExtraStaff();
            extraStaff.setExtraTime(date);
            extraStaff.setStaName(staff);
            extraDao.insertExtraStaff(extraStaff);
        }
        User user = (User) session.getAttribute("loginInfo");
        ExtraWork extraWork = new ExtraWork();
        extraWork.setExtraTime(date);
        extraWork.setExtraPublisher(String.valueOf(user.getUserId()));
        System.out.println(extraDao.insertExtraWork(extraWork));
    }

    //获取申请人名字
    @GetMapping("/get_sqr")
    @ResponseBody
    public Staff  getSqr(HttpSession session){
        User user = (User) session.getAttribute("loginInfo");
        Staff staff = staffDao.getStaffByName(user.getUserId());
        return staff;
    }
    //获取申请人所有值班时间
    @GetMapping("/get_sqr_date")
    @ResponseBody
    public List<ExtraStaff> getSqrDate(HttpSession session){
        User user = (User) session.getAttribute("loginInfo");
        Staff staff = staffDao.getStaffByName(user.getUserId());
        return extraDao.selectByName(staff.getStaName());
    }
    //获取所有值班时间
    @GetMapping("/get_all_date")
    @ResponseBody
    public List<ExtraWork> getAllDate(){
        return extraDao.selectAllDate();
    }
    //根据值班日期查人员名字
    @PostMapping("/select_by_date")
    @ResponseBody
    public List<ExtraStaff> selectByName(@RequestParam Date date){
        return extraDao.selectByDate(date);
    }
    //获取换班申请表
    @PostMapping("/change_work")
    @ResponseBody
    public void changeWork(@RequestParam String SQR,
                           @RequestParam Date oldDate,
                           @RequestParam Date newDate,
                           @RequestParam String HBR,
                           @RequestParam String HBYY,
                           @RequestParam char SFSH){
        //换班申请表
        ChangeWork changeWork = new ChangeWork();
        changeWork.setChangeApplyName(SQR);
        changeWork.setChangeNewDate(newDate);
        changeWork.setChangeNowDate(oldDate);
        changeWork.setReplaceName(HBR);
        changeWork.setChangeReason(HBYY);
        changeWork.setIsExamine(SFSH);
        System.out.println(changeWork);
        changeWorkDao.addChangeWork(changeWork);
    }
}
